/**
 * @Author     :lanbery
 * @e-mail     : lanbery@gmail.com
 * @version    : V1.0
 * @createtime : 2011-11-7-下午06:03:58
 */
package com.lambor.common.dao.impl;

import java.util.List;

import javax.inject.Inject;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

import com.lambor.common.dao.BaseMapperDao;
import com.lambor.common.mapper.BaseSqlMapper;

/**
 * @about this class :
 * @author           ： lanbery
 * @e-mail           : lanbery@gmail.com
 * @version          : V1.0
 * @modifytime       : 2011-11-7-下午06:03:58
 * @filepath         : SSI-com.lambor.common.dao.impl//BaseMapperDaoImpl.java
 *
 */
@SuppressWarnings("unchecked")
@Repository
public class BaseMapperDaoImpl<T> extends SqlSessionTemplate implements
		BaseMapperDao<T> {

    @Inject
    public BaseMapperDaoImpl(SqlSessionFactory sqlSessionFactory) {
        super(sqlSessionFactory);
    }

    private Class<? extends BaseSqlMapper> mapperClass;

    public void setMapperClass(Class<? extends BaseSqlMapper> mapperClass) {
        this.mapperClass = mapperClass;
    }

    public BaseSqlMapper<T> getMapper() {
        return this.getMapper(mapperClass);
    }

    public T add(T entity) throws Exception {
        boolean flag = false;
        try {
            this.getMapper().add(entity);
            flag = true;
        } catch (Exception e) {
            flag = false;
            throw e;
        }
        return entity;
    }

	/* (non-Javadoc)
	 * @see com.lambor.common.dao.BaseMapperDao#edit(java.lang.Object)
	 */
	public boolean edit(T entity) throws Exception {
	       boolean flag = false;
	        try {
	            this.getMapper().edit(entity);
	            flag = true;
	        } catch (Exception e) {
	            flag = false;
	            throw e;
	        }
	        return flag;
	}

	/* (non-Javadoc)
	 * @see com.lambor.common.dao.BaseMapperDao#queryLsit(java.lang.Object)
	 */
	public List<T> queryLsit(T entity) throws Exception {
		return this.getMapper().queryList(entity);
	}

	/* (non-Javadoc)
	 * @see com.lambor.common.dao.BaseMapperDao#queryTotalNum(java.lang.Object)
	 */
	public int queryTotalNum(T entity) throws Exception {
		return this.getMapper().queryTotalNum(entity);
	}

	/* (non-Javadoc)
	 * @see com.lambor.common.dao.BaseMapperDao#remove(java.lang.Object)
	 */
	public boolean remove(T entity) throws Exception {
		   boolean flag = false;
	        try {
	            this.getMapper().remove(entity);
	            flag = true;
	        } catch (Exception e) {
	            flag = false;
	            throw e;
	        }
	        return flag;
	}

	/* (non-Javadoc)
	 * @see com.lambor.common.dao.BaseMapperDao#selectOne(java.lang.Object)
	 */
	public T selectOne(T entity) throws Exception {
		return this.getMapper().selectOne(entity);
	}
}
